package com.zby.simulationHibernate.util.reflect.property;

import com.zby.simulationHibernate.constant.StandardBasicTypes;
import com.zby.simulationHibernate.util.common.CommonUtil;
import com.zby.simulationHibernate.util.exception.PropertyNotFoundException;
import com.zby.simulationHibernate.util.factory.TransformFactory;
import com.zby.simulationHibernate.util.reflect.PropertyUtil;
import com.zby.simulationHibernate.vo.Project;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created By zby on 11:05 2019/1/28
 * 通过属性名的数组和属性的类型数组获取值，也就用到了字典
 */

public class PropertyAccessor<T> {

    public PropertyAccessor() {
    }

    public Object getPropertyValue(T t, String propertyName) throws PropertyNotFoundException {
        Class<?> clazz = null;
        try {
            if (CommonUtil.isNotNull(t)) {
                clazz = t.getClass();
                Field field = clazz.getDeclaredField(propertyName);
                Type paramType = field.getGenericType();
                if (StandardBasicTypes.BOOLEAN.equalsIgnoreCase(paramType.toString())) {
                    //设置修改修改的权限，对于bool类型的数据，我们只能修改器属性
                    field.setAccessible(true);
                    return field.getBoolean(t) ? 1 : 0;
                }
                propertyName = PropertyUtil.propertyNameTransformerGetMethod(field.getGenericType(), propertyName);
                Method method = clazz.getMethod(propertyName);
                method.setAccessible(true);
                return method.invoke(t);
            }
            return null;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            System.out.println(clazz + "类下没有这样的方法");
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            System.out.println(clazz + "类下没有这样的方法");
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            System.out.println(clazz + "类下没有" + propertyName + "属性");
            e.printStackTrace();
        }
        return null;
    }

    public T setPropertyValue(T t, String propertyName, ResultSet resultSet, String columnName) throws PropertyNotFoundException {
        Class<?> clazz = null;
        try {
            clazz = t.getClass();
            Field field = clazz.getDeclaredField(propertyName);
            Type paramType = field.getGenericType();
            if (StandardBasicTypes.BOOLEAN.equalsIgnoreCase(paramType.toString())) {
                //设置修改修改的权限，对于bool类型的数据，我们只能修改器属性
                field.setAccessible(true);
                field.setBoolean(t, resultSet.getBoolean("is_" + columnName));
                return t;
            }
            String methodName = PropertyUtil.propertyNameTransformerSetMethod(propertyName);
            TransformFactory.createTransformFactory().transformType(clazz, t, resultSet, columnName, methodName, paramType);
            return t;
        } catch (SQLException e) {
            System.out.println("结果集异常resultSet=" + resultSet);
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            System.out.println("非法访问异常clazz=" + clazz);
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            System.out.println(clazz + "不存在属性propertyName" + propertyName);
            e.printStackTrace();
        }
        return null;
    }


    public static void main(String[] args) {
        Project project = new Project();
        project.setCustomerName("你好");
        project.setCustomerPhone("123454566");
        System.out.println(new PropertyAccessor().getPropertyValue(project, "customerName"));
    }
}
